package cn.sxt.userscenter.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;

import cn.sxt.userscenter.common.controller.BaseController;
import cn.sxt.userscenter.common.util.ConstatFinalUtil;
import cn.sxt.userscenter.common.util.PageInfoUtil;
import cn.sxt.userscenter.service.IUserService;
import cn.sxt.userscenter.users.pojo.AAdmins;
import cn.sxt.userscenter.users.pojo.AUsers;
import cn.sxt.userscenter.users.pojo.AUsersEnum;

@Controller
public class NoLoginController extends BaseController{
	
	@Resource
	private IUserService userService;
	
	@RequestMapping("/adminsList")
	public String adminsList(HttpServletRequest request){
		ConstatFinalUtil.LOGGER.info("---- adminsList ----");
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ; 
		Map<String, Object> condMap = new HashMap<String,Object>();
		List<AAdmins> adminsList = this.userService.findCondListAdminsService(pageInfoUtil, condMap);
		
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		return "/head/adminsList" ; 
		
	}
	@RequestMapping("/login")
	public String login(){
		ConstatFinalUtil.LOGGER.info("---- login ----");
		return "/head/login" ; 
	}
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request,HttpSession session,String id){
		ConstatFinalUtil.LOGGER.info("---- loginSubmit ----");
		String email=request.getParameter("email");
		String password=request.getParameter("password");
		String code=request.getParameter("code");
		//从session中取验证码
		String sessionCode=session.getAttribute("rand")+"";
		//为确保安全，验证码对比完成，立刻从session中移除
		session.removeAttribute("rand");
		
		if(sessionCode !=null && sessionCode.equalsIgnoreCase(code)){
			//根据用户名查询用户
			Map<String, Object> condMap = new HashMap<String, Object>();
			condMap.put("email", email);
			//ba把email放入查询条件，进行查询
			AUsers user=this.userService.findOneUsersService(condMap);
			//panduan fanhuidejieguo判断返回i的结果
			if(user !=null){
				//把传入的密码和查询得到的用户所对应的密码进行对比
				if(this.encryptUtil.decode(password, user.getPassword())){
					if(user.getStatus() == AUsersEnum.STATUS_ENABLE.getStatus()){
						//登陆成功，放入session
						session.setAttribute("user", user);
						session.setAttribute("getLastLoginTime", user.getLastLoginTime());
						//denglu登陆成功后把数据库中的上次登录时间修改
						user.setLastLoginTime(new Date());
						this.userService.updateOneUsersService(user);
						//跳转到登陆成功的页面
						return "redirect:/head/users/main.ios";
					}else{
						this.info="此账户被禁用";
					}					
				}else{
					this.info="密码不正确";
				}				
			}else{
				this.info="邮箱不存在";	
			}						
		}else{
			this.info="验证码不正确";
		}
		
		request.setAttribute("info", info);
		
		return this.login() ; 
	}
	
	
	//进入注册页面
	@RequestMapping("/regsiter")
	public String regsiter(){
		ConstatFinalUtil.LOGGER.info("---- regsiter ----");
		return "head/regsiter";
	}
	//注册
	@RequestMapping("/regsiterSubmit")
	public String regsiter(HttpServletRequest request,HttpSession session,String id){
		ConstatFinalUtil.LOGGER.info("---- regsiter ----");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		
		/* 从session中取验证码 */
		String sessCode = session.getAttribute("rand") + "";
		/* 验证码比对完成,从session中移除 */
		session.removeAttribute("rand");
		//验证码
		if(sessCode != null && sessCode.equalsIgnoreCase(code)){
			/* 入库 */
			AUsers users = new AUsers() ;
			users.setEmail(email);
			users.setPassword(this.encryptUtil.encode(password));
			/* 刚注册都是启用 */
			users.setStatus(AUsersEnum.STATUS_ENABLE.getStatus());
			
			users.setCreateTime(new Date());
			users.setUpdateTime(new Date());
			users.setLastLoginTime(new Date());
			
			JSONObject resultJSON = this.userService.saveOneUsersService(users);
			this.info = resultJSON.getString("info") ;
			
		}else{
			this.info="验证码不正确";
		}
		
		
		return this.regsiter();
	}
	
}
